package gebruikersinterface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import domein.DomeinController;

/**
* @author Stijn Vannieuwenhuyse
*/
@SuppressWarnings("serial")
public class Overzicht extends Hoofdscherm {
	
	private JPanel view;
	private int width = 700;
	private int height = 600;
	boolean koppel = true;
	
	public Overzicht(Scherm scherm){
		super(scherm);
		this.setLayout(new GridBagLayout());
		this.setBackground(new Color(240,240,240));
		
		this.view = new JPanel();
			view.setLayout(null);
			view.setPreferredSize(new Dimension(this.width, this.height));
			view.setBackground(new Color(240,240,240));		
		this.add(view);
		this.refresh();
	}

	public void refresh(){
		this.view.removeAll();
		
		String[][] resources = DomeinController.getInstance().getResources();
		StringTokenizer tokens;
		
		for(String[] res : resources){
			int id = Integer.parseInt(res[0]);
			final int resrc = id;
			String omschr = res[1];
			String coord = res[2];
			int bezet = Integer.parseInt(res[3]);
			final int rekening = bezet;
			
			tokens = new StringTokenizer(coord, ";");
				double x = Double.parseDouble(tokens.nextToken(";"));
				double y = Double.parseDouble(tokens.nextToken(";"));
				double w = Double.parseDouble(tokens.nextToken(";"));
				double h = Double.parseDouble(tokens.nextToken(";"));
				
			JButton resource = new JButton(omschr);
				resource.setBounds((int)(x*this.width), (int)(y*this.height), (int)(w*this.width), (int)(h*this.height));
				resource.setFont(new Font(null,Font.BOLD,16));
												
				//als huidige resource
				if(id == DomeinController.getInstance().getHuidigeResourceId()){
					resource.setForeground(new Color(255,255,255));
					resource.setBorder(new LineBorder(new Color(0,0,0),4));
				}
				else{
					resource.setForeground(new Color(0,0,0));
					resource.setBorder(new LineBorder(new Color(0,0,0),1));
				}
				
				//kleur
				resource.setBackground( bezet != 0 ? new Color(255,0,0) : new Color(0,255,0) );
				
				resource.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
							selecteerResourceAction(resrc, rekening);
					}
				});
				
				this.view.add(resource);
		}
		this.repaint();
	}
	
	public void selecteerResourceAction(int resource, int rekening){
		DomeinController.getInstance().setHuidigeResource(resource);
		if(rekening != 0) DomeinController.getInstance().setHuidigeRekening(rekening);
		super.scherm.refresh(rekening != 0 ? true : false);
		
	}
}